Ext.define('YX.core.TranslateMsg', {
	  extend : 'Ext.panel.Panel',
	  layout : {
		  type : 'hbox',
		  align : 'stretch'
	  },
	  gridMsg : null,
	  frmMsg : null,
	  txtSearchMsgCode : null,
	  txtSearchMsgDesc : null,
	  txtMsgCode : null,
	  cmbMsgType : null,
	  cmbLanguage : null,
	  txtMsgDesc : null,
	  rdoServerSide : null,
	  rdoClientSide : null,
	  btnSave : null,
	  btnDelete : null,
	  btnSearch : null,
	  btnReset : null,
	  initComponent : function() {
		  this.gridMsg = Ext.create('Ext.grid.Panel', {
			    flex : 1,
			    title : T.label.messageList,
			    store : {
				    xtype : 'json',
				    fields : ['msgCode', 'msgLocale', 'msgDesc', 'sideSign', {
					      type : 'date',
					      name : 'updateDate',
					      dateFormat : 'YmdHis'
				      }, 'updateUserId'],
				    proxy : {
					    type : 'zbusrpc',
					    module : 'BasViewTranslateMsgListService',
					    reader : {
						    type : 'json',
						    root : 'msgList',
						    totalProperty : 'totalSize'
					    }
				    }
			    },
			    columns : [{
				      xtype : 'rownumberer'
			      }, {
				      header : T.label.messageCode,
				      dataIndex : 'msgCode',
				      width : 120
			      }, {
				      header : T.label.language,
				      dataIndex : 'msgLocale',
				      renderer : function(value) {
					      return 'zh_CN' === value ? '中文' : 'English';
				      }
			      }, {
				      header : T.label.messageDesc,
				      dataIndex : 'msgDesc',
				      width : 250
			      }, {
				      header : T.label.messageSide,
				      dataIndex : 'sideSign',
				      renderer : function(value) {
					      return value === 'S' ? T.label.serverSide : T.label.clientSide;
				      }
			      }, {
				      header : T.label.updateTime,
				      dataIndex : 'updateDate',
				      xtype : 'datecolumn',
				      format : 'Y-m-d H:i:s',
				      width : 130
			      }, {
				      header : T.label.updateUser,
				      dataIndex : 'updateUserId'
			      }],
			    listeners : {
				    scope : this,
				    select : this.onGridMsgSelect
			    }
		    });

		  this.txtSearchMsgCode = Ext.create('Ext.form.Text', {
			    fieldLabel : T.label.messageCode,
			    labelWidth : 60,
			    listeners : {
				    scope : this,
				    specialkey : this.onTxtSearchMsgCodeSpecialKey
			    }
		    });
		  this.txtSearchMsgDesc = Ext.create('Ext.form.Text', {
			    fieldLabel : T.label.messageDesc,
			    labelWidth : 60,
			    listeners : {
				    scope : this,
				    specialkey : this.onTxtSearchMsgDescSpecialKey
			    }
		    });
		  this.txtMsgCode = Ext.create('Ext.form.Text', {
			    fieldLabel : T.label.messageCode,
			    name : 'msgCode',
			    labelWidth : 60,
			    allowBlank : false
		    });
		  this.cmbMsgType = Ext.create('Ext.form.field.ComboBox', {
			    fieldLabel : T.label.type,
			    labelWidth : 60,
			    submitValue : false,
			    itemId : 'cmbMsgType',
			    displayField : 'text',
			    valueField : 'value',
			    editable : false,
			    queryMode : 'local',
			    value : 'label',
			    store : Ext.create('Ext.data.JsonStore', {
				      fields : ['value', 'text'],
				      data : [{
					        value : '',
					        text : '-'
				        }, {
					        value : 'label',
					        text : 'label'
				        }, {
					        value : 'msg',
					        text : 'msg'
				        }]
			      })
		    });
		  this.cmbLanguage = Ext.create('Ext.form.field.ComboBox', {
			    fieldLabel : T.label.language,
			    labelWidth : 60,
			    name : 'msgLocale',
			    itemId : 'cmbLanguage',
			    displayField : 'text',
			    valueField : 'value',
			    editable : false,
			    value : 'zh_CN',
			    store : Ext.create('Ext.data.ArrayStore', {
				      fields : ['value', 'text'],
				      data : [['zh_CN', '中文'], ['en', 'English']]
			      })
		    });
		  this.txtMsgDesc = Ext.create('Ext.form.Text', {
			    fieldLabel : T.label.messageDesc,
			    labelWidth : 60,
			    name : 'msgDesc'
		    });
		  this.rdoClientSide = Ext.create('Ext.form.Radio', {
			    name : 'sideSign',
			    checked : true,
			    boxLabel : T.label.clientSide
		    });
		  this.rdoServerSide = Ext.create('Ext.form.Radio', {
			    name : 'sideSign',
			    boxLabel : T.label.serverSide
		    });

		  this.btnSave = Ext.create('Ext.button.Button', {
			    text : T.label.save,
			    itemId : 'btnSave',
			    iconCls : 'icon-disk',
			    scope : this,
			    handler : this.onBtnSaveClicked
		    });
		  this.btnDelete = Ext.create('Ext.button.Button', {
			    text : T.label.del,
			    itemId : 'btnDelete',
			    iconCls : 'icon-delete',
			    scope : this,
			    handler : this.onBtnDeleteClicked
		    });
		  this.btnSearch = Ext.create('Ext.button.Button', {
			    text : T.label.search,
			    itemId : 'btnSearch',
			    iconCls : 'icon-find',
			    scope : this,
			    handler : this.onBtnSearchClicked
		    });
		  this.btnReset = Ext.create('Ext.button.Button', {
			    text : T.label.reset,
			    itemId : 'btnReset',
			    iconCls : 'icon-clear',
			    scope : this,
			    handler : this.onBtnResetClicked
		    });

		  this.frmMsg = Ext.create('Ext.form.Panel', {
			  //width: 300,
			  flex : 1,
			  items : [{
				    xtype : 'fieldset',
				    layout : 'form',
				    title : T.label.search,
				    items : [this.txtSearchMsgCode, this.txtSearchMsgDesc]
			    }, {
				    xtype : 'fieldset',
				    layout : 'form',
				    title : T.label.operate,
				    items : [this.txtMsgCode, this.cmbMsgType, this.cmbLanguage,
				      this.txtMsgDesc, {
					      xtype : 'fieldcontainer',
					      fieldLabel : T.label.messageSide,
					      labelWidth : 60,
					      layout : 'hbox',
					      defaults : {
						      flex : 1
					      },
					      items : [this.rdoClientSide, this.rdoServerSide]
				      }]
			    }]
			   //,
			   //buttons: [this.btnSave, this.btnDelete, this.btnSearch, this.btnReset]
		   });

		  this.items = [{
			    xtype : 'panel',
			    flex : 1,
			    layout : {
				    type : 'vbox',
				    align : 'stretch'
			    },
			    items : [this.gridMsg, {
				      xtype : 'pagingtoolbar',
				      store : this.gridMsg.getStore(),
				      displayInfo : true
			      }]
		    }, {
			    xtype : 'panel',
			    width : 300,
			    layout : {
				    type : 'vbox',
				    align : 'stretch'
			    },
			    items : [this.frmMsg, {
				      xtype : 'toolbar',
				      items : [this.btnSave, this.btnDelete, this.btnSearch, this.btnReset]
			      }]
		    }];

		  this.callParent();
	  },
	  onGridMsgSelect : function(grid, record) {
		  this.frmMsg.loadRecord(record);
		  this.rdoClientSide.setValue(record.get('sideSign') === 'C');
		  this.rdoServerSide.setValue(record.get('sideSign') === 'S');
		  if (record.get('sideSign') === 'C') {
			  var msgCode = record.get('msgCode');
			  if (Ext.String.startsWith(msgCode, 'label.', true)) {
				  this.cmbMsgType.setValue('label');
			  } else if (Ext.String.startsWith(msgCode, 'msg.', true)) {
				  this.cmbMsgType.setValue('msg');
			  } else {
				  this.cmbMsgType.setValue('');
			  }

			  var index = msgCode.lastIndexOf(".");
			  if (index > 0) {
				  this.txtMsgCode.setValue(msgCode.substr(index + 1));
			  }
		  }
	  },
	  onBtnSaveClicked : function(btn) {
		  if (!this.frmMsg.getForm().isValid()) {
			  return;
		  }

		  if (this.cmbMsgType.getValue() != ""
		    && this.txtMsgCode.getValue().indexOf(" ") >= 0) {
			  Ext.Msg.show({
				    title : T.label.save,
				    msg : T.msg.msg_code_contains_blank_when_using_label_msg,
				    icon : Ext.Msg.ERROR,
				    buttons : Ext.Msg.OK
			    });
			  return;
		  }

		  Ext.Msg.show({
			    title : T.label.save,
			    msg : T.msg.are_you_sure_to_save_data,
			    icon : Ext.Msg.QUESTION,
			    buttons : Ext.Msg.YESNO,
			    scope : this,
			    fn : function(btnId) {
				    if (btnId == 'yes') {
					    btn.disable();
					    this.saveMsg(btn);
				    }
			    }
		    });
	  },
	  onBtnDeleteClicked : function(btn) {
		  if (Ext.isEmpty(this.gridMsg.getSelectionModel().getSelection())) {
			  return;
		  }

		  Ext.Msg.show({
			    title : T.label.del,
			    msg : T.msg.are_you_sure_to_delete_data,
			    icon : Ext.Msg.QUESTION,
			    buttons : Ext.Msg.YESNO,
			    scope : this,
			    fn : function(btnId) {
				    if (btnId == 'yes') {
					    btn.disable();
					    this.deleteMsg(btn);
				    }
			    }
		    });
	  },
	  onBtnSearchClicked : function() {
		  this.searchMsg();
	  },
	  onBtnResetClicked : function() {
		  this.frmMsg.getForm().reset();
	  },
	  saveMsg : function(btn) {
		  var frmVals = this.frmMsg.getValues();
		  var msg = {
			  msgCode : frmVals.msgCode,
			  msgDesc : frmVals.msgDesc,
			  msgLocale : frmVals.msgLocale,
			  sideSign : this.rdoClientSide.getValue() ? "C" : "S"
		  };

		  if (msg.sideSign === "C") {
			  var index = msg.msgCode.lastIndexOf(".");
			  if (index > 0) {
				  msg.msgCode = msg.msgCode.substr(index + 1);
			  }

			  if (this.cmbMsgType.getValue() !== '') {
				  msg.msgCode = this.cmbMsgType.getValue() + "." + msg.msgCode;
			  }
		  }

		  var parms = [];
		  parms.push(msg);

		  YX.ZbusRpc.request({
			    module : 'BasSaveTranslateMsgService',
			    scope : this,
			    params : {
				    msgList : parms
			    },
			    callback : function() {
				    btn.enable();
			    },
			    success : function() {
				    this.txtMsgDesc.setValue('');
				    this.txtSearchMsgCode.setValue(msg.msgCode);
				    this.searchMsg();
			    }
		    });
	  },
	  deleteMsg : function(btn) {
		  var rcds = this.gridMsg.getSelectionModel().getSelection();
		  var msg = {
			  msgCode : rcds[0].get('msgCode'),
			  msgLocale : rcds[0].get('msgLocale')
		  };

		  YX.ZbusRpc.request({
			    module : 'BasDeleteTranslateMsgService',
			    scope : this,
			    params : msg,
			    callback : function() {
				    btn.enable();
			    },
			    success : function() {
				    this.txtSearchMsgCode.setValue('');
				    this.txtMsgCode.setValue('');
				    this.txtMsgDesc.setValue('');
				    this.searchMsg();
			    }
		    });
	  },
	  onTxtSearchMsgCodeSpecialKey : function(field, e) {
		  if (e.getKey() == e.ENTER) {
			  this.searchMsg();
		  }
	  },
	  onTxtSearchMsgDescSpecialKey : function(field, e) {
		  if (e.getKey() == e.ENTER) {
			  this.searchMsg();
		  }
	  },
	  searchMsg : function() {
		  var gridStoreProxy = this.gridMsg.getStore().getProxy();
		  gridStoreProxy.setExtraParam('msgCode', this.txtSearchMsgCode.getValue());
		  gridStoreProxy.setExtraParam('msgDesc', this.txtSearchMsgDesc.getValue());
		  this.gridMsg.getStore().loadPage(1);
	  }
  });
